#include "adc_btn.h"


// ADC按键
int adc_key_val = -1;

#if CALL_METHOD
static void adc_key_scan(int voltage_val);
static void adc_key_task_start(void* data);
#else
static adc_collect_para adc_clct_para_timer;
static void timer_adc_key_scan(void);
static void timer_adc_key_func(void* data);
#endif

#if CALL_METHOD
static void adc_key_scan(int voltage_val)
{  
    if(voltage_val/100 < 25){
            switch (voltage_val/100) {
            case 3: adc_key_val = KEY_VOL_ADD; break;
            case 7 ... 8: adc_key_val = KEY_VOL_SUB; break;
            case 10 ... 11: adc_key_val = KEY_SET; break;
            case 16: adc_key_val = KEY_PLAY; break;
            case 19: adc_key_val = KEY_MUTE; break;
            case 23 ... 24: adc_key_val = KEY_REC; break;
            default: adc_key_val = -1; break;
        }
    }else{
         adc_key_val = -1;
    }
    
    printf("adc_key: %d\r\n", adc_key_val);
}

// ADC按键线程任务函数
static void adc_key_task_start(void* data)
{
    adc_collect_para adc_clct_para = *(adc_collect_para*)data;
    
    int adc_voltage_val = 0;
    while(1){
        printf("task adc btn\r\n");
        adc_voltage_val = adc1_read_voltage_val(&adc_clct_para.adc_handle, &adc_clct_para.adc_cali_chanx_handle);
        if(adc_voltage_val/100 < 25){
            vTaskDelay(pdMS_TO_TICKS(100)); // 延时参数需要根据板载的ADC按键调整
            adc_voltage_val = adc1_read_voltage_val(&adc_clct_para.adc_handle, &adc_clct_para.adc_cali_chanx_handle);
        }
        adc_key_scan(adc_voltage_val); // 进行按键扫描

        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

#else

static void timer_adc_key_scan(void)
{
    int voltage_val = adc1_read_voltage_val(&adc_clct_para_timer.adc_handle, &adc_clct_para_timer.adc_cali_chanx_handle);
    if(voltage_val/100 < 25){
        vTaskDelay(pdMS_TO_TICKS(100)); // 延时参数需要根据板载的ADC按键调整
        voltage_val = adc1_read_voltage_val(&adc_clct_para_timer.adc_handle, &adc_clct_para_timer.adc_cali_chanx_handle);
    }

    if(voltage_val/100 < 25){
        switch (voltage_val/100){
            case 3: adc_key_val = KEY_VOL_ADD; break;
            case 7 ... 8: adc_key_val = KEY_VOL_SUB; break;
            case 10 ... 11: adc_key_val = KEY_SET; break;
            case 16: adc_key_val = KEY_PLAY; break;
            case 19: adc_key_val = KEY_MUTE; break;
            case 23 ... 24: adc_key_val = KEY_REC; break;
            default: adc_key_val = -1; break;
        }
        //printf("66:adc_key: %d\r\n", adc_key_val);
    }else{
        adc_key_val = -1;
    }
}


/**
* @brief gui guider软件生成代码的"setup_scr_screen.c"文件
* 中"setup_scr_screen()"函数创建ADC按键定时扫描的函数后, 在该函数中实现你需要的ADC按键函数逻辑
* @param[in] void
* @return lv_timer_t类型,以便其他函数使用
*/
static void timer_adc_key_func(void* data)
{
    timer_adc_key_scan();
    
	if(adc_key_val == KEY_VOL_ADD){
		counter++;
		sprintf(buf,  "%d" , counter);
		lv_label_set_text(guider_ui.screen_counter, buf);
	}else if(adc_key_val == KEY_VOL_SUB){
		if(counter) counter--;
		sprintf(buf,  "%d" , counter);
		lv_label_set_text(guider_ui.screen_counter, buf);
	}
}

/**
* @brief gui guider软件生成代码的"setup_scr_screen.c"文件中
* "setup_scr_screen()"函数创建ADC按键定时扫描的函数
* @param[in] void
* @return lv_timer_t类型,以便其他函数使用
*/
lv_timer_t* create_timer_btn_start(void)
{
    lv_timer_t * timer = NULL;
    // 每过10ms启用一次该定时器
    timer = lv_timer_create((lv_timer_cb_t)timer_adc_key_func, 10,  NULL);
	timer->repeat_count = -1; // 无限循环启用该定时器
	lv_timer_resume(timer);

    return timer;
}

#endif

/**
* @brief ADC按键初始化, 创建线程进行按键扫描
* @return NULL
*/
void adc_key_init(void)
{
    adc_oneshot_unit_handle_t adc1_handle = NULL;
    adc_cali_handle_t adc1_cali_chan4_handle = NULL;

    adc_collect_para adc_clct_para = {
        .adc_handle = adc1_handle,
        .adc_cali_chanx_handle = adc1_cali_chan4_handle
    };

    adc1_init(&adc_clct_para.adc_handle, &adc_clct_para.adc_cali_chanx_handle);

#if CALL_METHOD
    // 创建一个ADC按键扫描线程
    xTaskCreate(adc_key_task_start, "adc_key_test", 1024 * 4, (void*)&adc_clct_para, 1, NULL);
#else
    memcpy(&adc_clct_para_timer, &adc_clct_para, sizeof(adc_collect_para));
#endif
}







